Public Class Form1

    Private Sub cmdcheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcheck.Click
        'Variable used to store the check digit as it is calculated
        Dim iNumber As Integer

        'This runs through adding the weight of each digit to the running total.
        iNumber += 3 * Val(txtbarcode1.Text)
        iNumber += 1 * Val(txtbarcode2.Text)
        iNumber += 3 * Val(txtbarcode3.Text)
        iNumber += 1 * Val(txtbarcode4.Text)
        iNumber += 3 * Val(txtbarcode5.Text)
        iNumber += 1 * Val(txtbarcode6.Text)
        iNumber += 3 * Val(txtbarcode7.Text)

        'Mod the result of the weights to get the remainder
        iNumber = iNumber Mod 10

        'Finally subtract 10 to leave the check digit
        iNumber = 10 - iNumber

        'Check the calculated check digit against the given one and report either valid or invalid
        If (iNumber = Val(txtbarcode8.Text)) Then
            MessageBox.Show("Valid", "Barcode Checker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            MessageBox.Show("Invalid", "Barcode Checker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub cmdcheckean13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcheckean13.Click
        'Variable used to store the check digit as it is calculated
        Dim iNumber As Integer

        'Total all the weight calculations for the check digit
        iNumber += 3 * Val(txtean13_1.Text)
        iNumber += 1 * Val(txtean13_2.Text)
        iNumber += 3 * Val(txtean13_3.Text)
        iNumber += 1 * Val(txtean13_4.Text)
        iNumber += 3 * Val(txtean13_5.Text)
        iNumber += 1 * Val(txtean13_6.Text)
        iNumber += 3 * Val(txtean13_7.Text)
        iNumber += 1 * Val(txtean13_8.Text)
        iNumber += 3 * Val(txtean13_9.Text)
        iNumber += 1 * Val(txtean13_10.Text)
        iNumber += 3 * Val(txtean13_11.Text)
        iNumber += 1 * Val(txtean13_12.Text)

        'Mod the result to find the remainder
        iNumber = iNumber Mod 10

        'Subtract from  10 to get the check digit
        iNumber = 10 - iNumber

        'Check the calculated check digit against the given check digit
        'and report either vaild or invalid
        If (iNumber = Val(txtean13_13.Text)) Then
            MessageBox.Show("Valid", "Barcode Checker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            MessageBox.Show("Invalid", "Barcode Checker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub cmdCheckISBN10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckISBN10.Click
        'Variable to store the calculated check digit
        Dim iCheck As Integer

        'Calculate the weighting system
        'Note this is much longer than before as you have to take into
        'account the X symbol meaning 10
        If (txtisbn10_1.Text = "X") Then
            iCheck += 10 * 10
        Else
            iCheck += Val(txtisbn10_1.Text) * 10
        End If

        If (txtisbn10_2.Text = "X") Then
            iCheck += 10 * 9
        Else
            iCheck += Val(txtisbn10_2.Text) * 9
        End If

        If (txtisbn10_3.Text = "X") Then
            iCheck += 10 * 8
        Else
            iCheck += Val(txtisbn10_3.Text) * 8
        End If

        If (txtisbn10_4.Text = "X") Then
            iCheck += 10 * 7
        Else
            iCheck += Val(txtisbn10_4.Text) * 7
        End If

        If (txtisbn10_5.Text = "X") Then
            iCheck += 10 * 6
        Else
            iCheck += Val(txtisbn10_5.Text) * 6
        End If

        If (txtisbn10_6.Text = "X") Then
            iCheck += 10 * 5
        Else
            iCheck += Val(txtisbn10_6.Text) * 5
        End If

        If (txtisbn10_7.Text = "X") Then
            iCheck += 10 * 4
        Else
            iCheck += Val(txtisbn10_7.Text) * 4
        End If

        If (txtisbn10_8.Text = "X") Then
            iCheck += 10 * 3
        Else
            iCheck += Val(txtisbn10_8.Text) * 3
        End If

        If (txtisbn10_9.Text = "X") Then
            iCheck += 10 * 2
        Else
            iCheck += Val(txtisbn10_9.Text) * 2
        End If

        'Mod the result to find the remainder
        iCheck = iCheck Mod 11

        'Subtract from 11 to get the check digit
        iCheck = 11 - iCheck

        'Compair to the given check digit
        'Again an additional piece of code has had to be added to take into account the X symbol
        If (iCheck = Val(txtisbn10_10.Text)) Then
            MessageBox.Show("Valid", "Barcode Checker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ElseIf (iCheck = 10 And txtisbn10_10.Text = "X") Then
            MessageBox.Show("Valid", "Barcode Checker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            MessageBox.Show("Invalid", "Barcode Checker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub

    Private Sub txtClearEAN8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClearEAN8.Click
        'Set each textbox to no text clearing them
        txtbarcode1.Text = ""
        txtbarcode2.Text = ""
        txtbarcode3.Text = ""
        txtbarcode4.Text = ""
        txtbarcode5.Text = ""
        txtbarcode6.Text = ""
        txtbarcode7.Text = ""
        txtbarcode8.Text = ""
    End Sub

    Private Sub txtClearEAN13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClearEAN13.Click
        'Set each textbox to no text clearing them
        txtean13_1.Text = ""
        txtean13_2.Text = ""
        txtean13_3.Text = ""
        txtean13_4.Text = ""
        txtean13_5.Text = ""
        txtean13_6.Text = ""
        txtean13_7.Text = ""
        txtean13_8.Text = ""
        txtean13_9.Text = ""
        txtean13_10.Text = ""
        txtean13_11.Text = ""
        txtean13_12.Text = ""
        txtean13_13.Text = ""
    End Sub

    Private Sub txtClearISBN10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClearISBN10.Click
        'Set each textbox to no text clearing them
        txtisbn10_1.Text = ""
        txtisbn10_2.Text = ""
        txtisbn10_3.Text = ""
        txtisbn10_4.Text = ""
        txtisbn10_5.Text = ""
        txtisbn10_6.Text = ""
        txtisbn10_7.Text = ""
        txtisbn10_8.Text = ""
        txtisbn10_9.Text = ""
        txtisbn10_10.Text = ""
    End Sub
End Class
